package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.Packing;
import cn.itcast.domain.cargo.PackingExample;
import cn.itcast.domain.cargo.Shipping;
import cn.itcast.domain.cargo.ShippingExample;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.service.cargo.ShippingService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/cargo/shipping")
public class ShippingController extends BaseController {
    @Reference
    private ShippingService shippingService;

    @Reference
    private PackingService packingService;

    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5") int size) {
        //根据企业id分页查询委托单列表
        ShippingExample shippingExample = new ShippingExample();
        ShippingExample.Criteria criteria = shippingExample.createCriteria();
        criteria.andCompanyIdEqualTo(getLoginCompanyId());
        PageInfo info = shippingService.findAll(page, size, shippingExample);
        request.setAttribute("page", info);
        return "cargo/shipping/shipping-list";
    }

    @RequestMapping("/toAdd")
    public String toAdd() {
        //查询状态为0的装箱单，只有未委托的装箱单才能进行委托
        PackingExample packingExample = new PackingExample();
        PackingExample.Criteria criteria = packingExample.createCriteria();
        criteria.andCompanyIdEqualTo(getLoginCompanyId());
        criteria.andStateEqualTo(0);
        List<Packing> list = packingService.findAll(packingExample);
        request.setAttribute("list", list);
        return "cargo/shipping/shipping-add";
    }

    @RequestMapping("/edit")
    public String edit(Shipping shipping) {
        shipping.setCompanyId(getLoginCompanyId());
        shipping.setCompanyName(getLoginCompanyName());
        shippingService.save(shipping);
        return "redirect:/cargo/shipping/list.do";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public int delete(String id) throws IOException {
        //与ajax交互，已出票的委托单不能删除
        int flag = shippingService.delete(id);
        return flag;
    }

    //下载Excel
    @RequestMapping("/printTemplate")
    public void printTemplate(String id) throws IOException {
        //准备数据
        Shipping shipping = shippingService.findById(id);
        Packing packing = packingService.findById(id);
        Map map = new HashMap<>();
        map.put("notifyParty", shipping.getNotifyParty());
        map.put("id", id);
        map.put("loadingDate", shipping.getLoadingDate());
        map.put("lcNo", shipping.getLcNo());
        map.put("portOfLoading", shipping.getPortOfLoading());
        map.put("portOfDischarge", shipping.getPortOfDischarge());
        map.put("portOfTrans", "none");
        map.put("marks", packing.getMarks());
        map.put("grossWeight", packing.getGrossWeights());
        map.put("volume", packing.getTotalVolume());
        map.put("specialCondition", shipping.getSpecialCondition());

        //配置下载信息
        response.setContentType("application/vnd.ms-excel"); //下载excel
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("委托单", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

        //加载模板以及创建EasyExcel填充模板
        String path = session.getServletContext().getRealPath("/")+"/make/shipping_order.xlsx";
        ExcelWriter ew = EasyExcel.write(response.getOutputStream()).withTemplate(path).build();
        WriteSheet sheet = EasyExcel.writerSheet().build();
        ew.fill(map, sheet);
        ew.finish();
    }
}
